#!/usr/bin/env python3
"""
Main entry point for the Python remote control system
Provides a simple CLI interface to start capture or playback
"""

import sys
import argparse
from colorama import init, Fore, Style
from capture import InputCapture
from playback import InputPlayback
import config

# Initialize colorama
init()

def show_help():
    """Show help information"""
    print(f"{Fore.MAGENTA}🖱️  Python Remote Control System{Style.RESET_ALL}")
    print(f"{Fore.MAGENTA}=================================={Style.RESET_ALL}")
    print()
    print("Usage:")
    print(f"  {Fore.CYAN}python main.py capture [host] [port]{Style.RESET_ALL}    - Start input capture")
    print(f"  {Fore.CYAN}python main.py playback [port]{Style.RESET_ALL}          - Start input playback")
    print(f"  {Fore.CYAN}python main.py help{Style.RESET_ALL}                     - Show this help")
    print()
    print("Examples:")
    print(f"  {Fore.GREEN}python main.py capture{Style.RESET_ALL}                  - Capture to localhost:3001")
    print(f"  {Fore.GREEN}python main.py capture 192.168.1.100{Style.RESET_ALL}    - Capture to remote host")
    print(f"  {Fore.GREEN}python main.py playback{Style.RESET_ALL}                 - Listen on port 3001")
    print(f"  {Fore.GREEN}python main.py playback 3002{Style.RESET_ALL}            - Listen on port 3002")
    print()
    print("Direct commands:")
    print(f"  {Fore.YELLOW}python capture.py [host] [port]{Style.RESET_ALL}        - Direct capture")
    print(f"  {Fore.YELLOW}python playback.py [port]{Style.RESET_ALL}              - Direct playback")
    print()
    print("Requirements:")
    print(f"  {Fore.BLUE}pip install -r requirements.txt{Style.RESET_ALL}")

def main():
    """Main function"""
    parser = argparse.ArgumentParser(
        description='Python Remote Control System',
        add_help=False  # We'll handle help ourselves
    )
    parser.add_argument('command', nargs='?', help='Command to run (capture/playback/help)')
    parser.add_argument('args', nargs='*', help='Additional arguments')
    
    args = parser.parse_args()
    
    if not args.command or args.command in ['help', '--help', '-h']:
        show_help()
        return
    
    command = args.command.lower()
    
    if command == 'capture':
        host = args.args[0] if len(args.args) > 0 else config.NETWORK['default_host']
        port = int(args.args[1]) if len(args.args) > 1 else config.NETWORK['default_port']
        
        print(f"{Fore.CYAN}🎯 Starting Capture Mode{Style.RESET_ALL}")
        print(f"Target: {host}:{port}")
        print()
        
        capture = InputCapture(host, port)
        try:
            capture.start()
            # Keep the main thread alive
            while capture.is_capturing:
                import time
                time.sleep(1)
        except KeyboardInterrupt:
            print(f"\n{Fore.YELLOW}🔄 Shutting down capture...{Style.RESET_ALL}")
            capture.stop()
        except Exception as e:
            print(f"{Fore.RED}💥 Error: {e}{Style.RESET_ALL}")
            capture.stop()
    
    elif command == 'playback':
        port = int(args.args[0]) if len(args.args) > 0 else config.NETWORK['default_port']
        
        print(f"{Fore.CYAN}🎮 Starting Playback Mode{Style.RESET_ALL}")
        print(f"Listening on port: {port}")
        print()
        
        playback = InputPlayback(port)
        try:
            playback.start()
        except KeyboardInterrupt:
            print(f"\n{Fore.YELLOW}🔄 Shutting down playback...{Style.RESET_ALL}")
            playback.stop()
        except Exception as e:
            print(f"{Fore.RED}💥 Error: {e}{Style.RESET_ALL}")
            playback.stop()
    
    else:
        print(f"{Fore.RED}❌ Invalid command: {command}{Style.RESET_ALL}")
        print()
        show_help()
        sys.exit(1)

if __name__ == '__main__':
    main()